import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VCharts from 'v-charts'
import { Message } from 'element-ui'
import { $post } from "@/utils/api.js";
import { $get } from "@/utils/api.js";
import { $put } from "@/utils/api.js";
import { $delete } from "@/utils/api.js";

Vue.config.productionTip = false

Vue.use(ElementUI);
Vue.use(VCharts);

Vue.prototype.$post = $post
Vue.prototype.$get = $get
Vue.prototype.$put = $put
Vue.prototype.$delete = $delete

// 全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/') {
    next()
  } else {
    let token = sessionStorage.getItem('tokenStr');
    if (token === null || token === '') {
      Message.warning("请先登录");
      next('/login')
    } else {
      if (!sessionStorage.getItem('user')) {
        $get('admin/detail').then((res) => {
          if (res) {
            sessionStorage.setItem('user', JSON.stringify(res))
            next()
          }
        })
      }
      next()
    }
  }
});

new Vue({
  render: h => h(App),
  router
}).$mount('#app')

